<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>系统字典表</title>
    <% include("/common/common_css_js.html"){} %>
</head>
<body>

    <div class = "mini-fit">
        <div style="width: 100%;height: 100%">
            <table style="width: 100%; height: 100%;table-layout: fixed;">
                <tr>
                    <td style="width: 50%">
                        <div id="p1" class="mini-panel" title="一级节点" style="width: 100%; height: 100%;" onbuttonclick="">
                            <div class="uc-toolbaroverflow" _borderStyle="border-top:0;">
                                <span>关键字：</span>
                                <input  id="key1" name="key1" class="mini-textbox" emptyText="ID/字段名/KEY"/>
                                <a class="mini-button" iconCls="icon-search" onclick="search1()">查询</a>
                                <span class="separator"></span>

                                <% if(so.hasPermission("xtdic:addLevel1Node")){%>
                                    <a class="mini-button" iconCls="icon-add" onclick="addLevel1Node()">新建一级节点</a>
                                <% } %>

                                <% if(so.hasPermission("xtdic:updateLevel1Node")){%>
                                    <a class="mini-button" iconCls="icon-edit" onclick="updateLevel1Node()">修改一级节点</a>
                                <% } %>

                                <% if(so.hasPermission("xtdic:refreashCache")){%>
                                    <a class="mini-button" iconCls="icon-edit" onclick="refreashCache()">刷新缓存</a>
                                <% } %>

                                <% if(so.hasPermission("xtdic:deleteXtDic1")){%>
                                    <a class="mini-button" iconCls="icon-no" onclick="deleteXtDic1()">删除</a>
                                <% } %>
                                <span class="separator"></span>
                            </div>
                            <!-- 父表    -->
                            <div id="left_grid" url="${base}/pages/basic_data_manage/xt_dic/query?isLeft=true"
                                 style="width: 100%; " class="mini-datagrid" autoLoad="true"
                                 onselectionchanged="onSelectionChangedLeft" selectOnLoad="true" pageSize="50" sizeList="[10,20,30,50,100]">
                                <div property="columns">
                                    <div field="id" width="10" headerAlign="left" align="left" allowSort="true">ID</div>
                                    <div field="name" width="20" headerAlign="left" align="left" allowSort="true">字段名</div>
                                    <div field="fullName" width="20" headerAlign="left" align="left" allowSort="true">全名</div>
                                    <div field="pid" width="20" headerAlign="left" align="left" allowSort="true">PID</div>
                                    <div field="dicKey" width="40" headerAlign="left" align="left" allowSort="true">key(勿改)</div>
                                    <div field="status" width="15" headerAlign="left" align="left" allowSort="true" data-options='{cacheName:"sysDic"}' renderer="commonRender">状态</div>
                                    <div field="standby1" width="20" visible="false">standby1</div>
                                    <div field="standby1Desc" width="20" visible="false">standby1Desc</div>
                                    <div field="standby2" width="20" visible="false">standby2</div>
                                    <div field="standby2Desc" width="20" visible="false">standby2Desc</div>
                                    <div field="standby3" width="20" visible="false">standby3</div>
                                    <div field="standby3Desc" width="20" visible="false">standby3Desc</div>
                                </div>
                            </div>

                        </div>
                    </td>
                    <td style="width: 50%">
                        <div id="p2" class="mini-panel" title="二级节点" style="width: 100%; height: 100%;" onbuttonclick="">
                            <div class="uc-toolbaroverflow" _borderStyle="border-top:0;">
                                <span>关键字：</span>
                                <input  id="key2" name="key2" class="mini-textbox"  emptyText="ID/字段名/KEY" />

                                <% if(so.hasPermission("xtdic:search2")){%>
                                    <a class="mini-button" iconCls="icon-search" onclick="search2()">查询</a>
                                <% } %>

                                <span class="separator"></span>

                                <% if(so.hasPermission("xtdic:addLevel2Node")){%>
                                    <a class="mini-button" iconCls="icon-add" onclick="addLevel2Node()">新建二级节点</a>
                                <% } %>

                                <% if(so.hasPermission("xtdic:updateLevel2Node")){%>
                                    <a class="mini-button" iconCls="icon-add" onclick="updateLevel2Node()">修改二级节点</a>
                                <% } %>

                                <% if(so.hasPermission("xtdic:deleteXtDicChild")){%>
                                    <a class="mini-button" iconCls="icon-no" onclick="deleteXtDicChild()">删除</a>
                                <% } %>
                            </div>

                            <!--子表  -->
                            <div id="right_grid" url="${base}/pages/basic_data_manage/xt_dic/query?isLeft=false" class="mini-treegrid"
                                 style="width: 100%; " idField="id" parentField="pid" showTreeIcon="true" treeColumn="taskname" dataField="data" resultAsTree="false"
                                 expandOnLoad="true" showPager="true"  pageSize="20" sizeList="[10,20,30,50,100]">
                                <div property="columns">
                                    <div field="name" width="20" headerAlign="left" align="left" allowSort="true">字段名</div>
                                    <div field="fullName" width="40" headerAlign="left" align="left" allowSort="true">全名</div>
                                    <div field="id" width="10" headerAlign="left" align="left" allowSort="true">ID</div>
                                    <div field="pid" width="10" headerAlign="left" align="left" allowSort="true">PID</div>
                                    <div field="dicKey" width="10" headerAlign="left" align="left" allowSort="true">key(勿改)</div>
                                    <div field="status" width="10" headerAlign="left" align="left" allowSort="true" data-options='{cacheName:"sysDic"}' renderer="commonRender">状态</div>
                                    <div field="standby1" width="20" visible="false">standby1</div>
                                    <div field="standby1Desc" width="20" visible="false">standby1Desc</div>
                                    <div field="standby2" width="20" visible="false">standby2</div>
                                    <div field="standby2Desc" width="20" visible="false">standby2Desc</div>
                                    <div field="standby3" width="20" visible="false">standby3</div>
                                    <div field="standby3Desc" width="20" visible="false">standby3Desc</div>
                                </div>
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
        </div>

        <!--编辑窗口-->
        <!-- 新建面板 -->
        <div id="systemDicPanel" class="mini-window" title="字典管理" style="width:700px;height:400px;"
             showMaxButton="true" showCollapseButton="false" showMinButton="true" showShadow="true"
             showToolbar="true" showFooter="true" showModal="true" allowResize="true" allowDrag="true">
            <div property="footer" style="padding:10px;text-align: right">
                <a  id="updateAndSaveBtn" name="updateAndSaveBtn" class="mini-button" iconCls="icon-save" onclick="updateAndSave()">保存</a>
                <a  id="addAndSaveBtn" name="addAndSaveBtn" class="mini-button" iconCls="icon-save" onclick="addAndSave()">保存</a>
            </div>
            <div id="systemDicForm">
                <table style="width: 100%">
                    <tr>
                        <td><label>ID:</label></td>
                        <td colspan="3"><input id="id" name="id"  class="mini-textbox" required="true" requiredErrorText="系统字典ID不能为空" emptyText="请输入ID，注意避免重复" vtype="int"/></td>
                    </tr>
                    <tr>
                        <td><label>名称:</label></td>
                        <td><input id="name" name="name"  class="mini-textbox" required="true" requiredErrorText="名称不能为空" emptyText="请输入名称"/></td>
                        <td><label>全名</label></td>
                        <td><input id="fullName" name="fullName"  class="mini-textbox" required="false" requiredErrorText="全名不能为空" emptyText="请输入全名"/></td>
                    </tr>
                    <tr>
                        <td><label>PID:</label></td>
                        <td><input id="pid" name="pid"  class="mini-textbox" required="true" requiredErrorText="PID不能为空" emptyText="请输入PID" vtype="int"/></td>
                        <td><label>Key:</label></td>
                        <td><input id="dicKey" name="dicKey"  class="mini-textbox" required="false" requiredErrorText="Key不能为空" emptyText="请输入Key,注意避免重复"/></td>
                    </tr>
                    <tr>
                        <td><label>状态:</label></td>
                        <td colspan="3"><input id="status" name="status" class="mini-combobox" style="width:150px;" textField="name" valueField="id" emptyText="请选择..."
                                   required="true" allowInput="false" showNullItem="true" nullItemText="请选择..." requiredErrorText="没有是否有效" emptyText="请输入状态"/></td>
                    </tr>
                    <tr>
                        <td><label>备用字段1:</label></td>
                        <td><input id="standby1" name="standby1"  class="mini-textbox" required="false" requiredErrorText="备用字段1不能为空" emptyText="备用字段1"/></td>
                        <td><label>备用字段1说明:</label></td>
                        <td><input id="standby1Desc" name="standby1Desc"  class="mini-textbox" required="false" requiredErrorText="备用字段1说明不能为空" emptyText="备用字段1说明"/></td>
                    </tr>
                    <tr>
                        <td><label>备用字段2:</label></td>
                        <td><input id="standby2" name="standby2"  class="mini-textbox" required="false" requiredErrorText="备用字段2不能为空" emptyText="备用字段2"/></td>
                        <td><label>备用字段2说明:</label></td>
                        <td><input id="standby2Desc" name="standby2Desc"  class="mini-textbox" required="false" requiredErrorText="备用字段2说明不能为空" emptyText="备用字段2说明"/></td>
                    </tr>
                    <tr>
                        <td><label>备用字段3:</label></td>
                        <td><input id="standby3" name="standby3"  class="mini-textbox" required="false" requiredErrorText="备用字段3不能为空" emptyText="备用字段3"/></td>
                        <td><label>备用字段3说明:</label></td>
                        <td><input id="standby3Desc" name="standby3Desc"  class="mini-textbox" required="false" requiredErrorText="备用字段3说明不能为空" emptyText="备用字段3说明"/></td>
                    </tr>
                </table>
            </div>
        </div>
        <!-- 隐藏域 -->
        <!-- 左边根id -->
        <input id="leftRootId" name="leftRootId" value="${leftRootId!}" class="mini-hidden" />
        <!-- 左边选择的某个条目的id -->
        <input id="leftSelectedId" name="leftSelectedId" class="mini-hidden" />
        <!-- 是否显示右边Grid -->
        <input id="isDisplayRight" name="isDisplayRight" value="${isDisplayRight!}" class="mini-hidden" />
        <!-- 所做的操作 -->
        <input id="oper" name="oper" class="mini-hidden" />

    </div>

    <script type="text/javascript">
        mini.parse();

        let leftRootId = mini.get("leftRootId");
        let leftSelectedId = mini.get("leftSelectedId");
        let isDisplayRight = mini.get("isDisplayRight");
        let oper = mini.get("oper");

        let key1 = mini.get("key1");
        let key2 = mini.get("key2");

        let leftGrid = mini.get("left_grid");
        let rightGrid = mini.get("right_grid");

        let grid;


        let systemDicPanel = mini.get("systemDicPanel");
        let updateAndSaveBtn = mini.get("updateAndSaveBtn");
        let addAndSaveBtn = mini.get("addAndSaveBtn");

        let form = new mini.Form("#systemDicForm");



        $(function () {
            // 默认加载
            leftGrid.load({id:"",name:"",pid:0,key:""});

            // 加载弹出窗口的有效性下拉列表  1	0	有效标志	XT_DIC_STATUS
            let jsonData = commonRenderSysDicDropListByKey("sysDic","XT_DIC_STATUS");
            mini.get("status").load(jsonData);

        });

        function onSelectionChangedLeft(e) {
            let grid = e.sender;
            let selectRow = grid.getSelected();//得到record对象
            if (selectRow) {
                //将父表的id放到隐藏域中
                leftSelectedId.setValue(selectRow.id);
                rightGrid.load({pid: selectRow.id });//获得一级节点的id
            }
        }

        function search1() {
            let keyValue = key1.getValue();
            // 左表格检索
            leftGrid.load({id : keyValue, name : keyValue, pid : 0, dicKey : keyValue});
        }

        function search2() {
            let keyValue = key2.getValue();
            // 左表格检索
            rightGrid.load({id : keyValue, name : keyValue, pid : leftSelectedId.getValue(), dicKey : keyValue});
        }

        // 新建一级节点
        function addLevel1Node() {
            form.clear();
            grid = leftGrid;
            addAndSaveBtn.show();
            updateAndSaveBtn.hide();
            // 设置key是必须的
            mini.get("dicKey").set({required :true});
            //设置id允许输入
            mini.get("id").setEnabled(true);
            //有效无效默认值为有效
            mini.get("status").setValue(2);

            // 弹出添加窗口
            mini.get("systemDicPanel").setTitle("新建一级节点");
            public_showModelAtPos("systemDicPanel", "center", "middle");
        }

        // 更新一级节点
        function updateLevel1Node() {
            form.clear();
            grid = leftGrid;
            // 获取当前选中的行
            let row = leftGrid.getSelected();

            if(!row){
                mini.alert("请选择要编辑的行!","字典编辑",null);
                return;
            }

            form.setData(row);

            addAndSaveBtn.hide();
            updateAndSaveBtn.show();

            // 设置key是必须的
            mini.get("dicKey").set({required :true});
            //设置id不允许修改
            mini.get("id").setEnabled(false);

            // 显示编辑面板
            mini.get("systemDicPanel").setTitle("更新一级节点");
            public_showModelAtPos("systemDicPanel", "center", "middle");
        }


        // 新建二级节点
        function addLevel2Node() {
            form.clear();
            addAndSaveBtn.show();
            updateAndSaveBtn.hide();
            grid = rightGrid;

            // 设置二级节点key不是必须的
            mini.get("dicKey").set({required :false});
            mini.get("pid").setValue(leftSelectedId.getValue());
            //有效无效默认值为有效
            mini.get("status").setValue(2);

            // 弹出添加窗口
            mini.get("systemDicPanel").setTitle("新建二级节点");
            public_showModelAtPos("systemDicPanel", "center", "middle");
        }

        // 更新二级节点
        function updateLevel2Node() {
            form.clear();

            grid = rightGrid;
            // 获取当前选中的行
            let row = rightGrid.getSelected();

            if(!row){
                mini.alert("请选择要编辑的行!","字典编辑",null);
                return;
            }


            form.setData(row);

            addAndSaveBtn.hide();
            updateAndSaveBtn.show();
            // 设置二级节点key不是必须的
            mini.get("dicKey").set({required :false});

            // 显示编辑面板
            mini.get("systemDicPanel").setTitle("更新二级节点");
            public_showModelAtPos("systemDicPanel", "center", "middle");
        }

        // 更新并保存
        function updateAndSave() {
            form.validate();
            if (form.isValid() == false) return;
            let data = form.getData(true);

            let ret = public_getServerData("${base}/pages/basic_data_manage/xt_dic/update",data);
            showAlert(ret.message,function (){
                if(ret.ok){
                    public_showTip(ret);
                    // 重新刷新数据
                    if(grid==leftGrid){
                        leftGrid.load({id:"",name:"",pid:0,key:""});
                    }
                    else{
                        rightGrid.load({pid: leftSelectedId.getValue() });//获得一级节点的id
                    }
                    public_onCancel('systemDicPanel');
                }
            });
        }

        // 添加并保存
        function addAndSave() {
            // 添加并保存
            form.validate();
            if (form.isValid() == false) return;
            let data = form.getData(true);

            let ret = public_getServerData("${base}/pages/basic_data_manage/xt_dic/insert",data);
            public_showTip(ret);
            if(ret.ok) {
                // 重新刷新数据
                if(grid == leftGrid){
                    leftGrid.load({id:"",name:"",pid:0,key:""});
                }else{
                    rightGrid.load({pid: leftSelectedId.getValue() });//获得一级节点的id
                }
                public_onCancel('systemDicPanel');
            }
        }


        // 刷新缓存，把刷新的缓存放到父页面，应当调用父级页面方法刷新
        function refreashCache() {
            parent.window.showMsgBox();
            parent.window.loadCache();
        }
        function deleteXtDic1(){
            grid = leftGrid;
            // 获取当前选中的行
            let row = leftGrid.getSelected();
            console.log(row.id);
            deleteDic(row.id);
        }
        function deleteXtDicChild(){
            grid = rightGrid;
            // 获取当前选中的行
            let row = rightGrid.getSelected();
            deleteDic(row.id);
        }

        // 更新并保存
        function deleteDic(id) {
            mini.confirm(
                "确定删除系统字典？",
                "确定？",
                function(action) {
                    if (action == "ok") {
                        let ret = public_getServerData("${base}/pages/basic_data_manage/xt_dic/delete?id=" + id);
                        public_showTip(ret);
                        if(ret.ok) {
                            // 重新刷新数据
                            if(grid == leftGrid){
                                leftGrid.load({id:"",name:"",pid:0,key:""});
                            }
                            else{
                                rightGrid.load({pid: leftSelectedId.getValue() });//获得一级节点的id
                            }
                        }
                    }
                });
        }



    </script>
</body>
</html>